package com.aliyun.oss.controller;

import com.aliyun.oss.service.AliyunOssService;
import com.aliyun.oss.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * Aliyun OSS 服务控制器
 * @author Hjj
 */
@RestController
@RequestMapping("/oss/file")
@Api(value = "API - AliyunOssController", description = "阿里云OSS文件云存储服务接口详情")
public class AliyunOssController {

    @Autowired
    private AliyunOssService aliyunOssService;

    @ApiOperation(value = "简易文件上传")
    @PostMapping("/upload")
    public R upload(
            @ApiParam(value = "MultipartFile" ,required = true)
            @RequestParam(value = "file") MultipartFile file ,
            @ApiParam(value = "oss文件管理的文件夹名称(模块名)" ,required = true)
            @RequestParam(value = "modules") String modules) throws IOException {

        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String url = aliyunOssService.ossFileUpload(originalFilename, modules, inputStream);
        return R.ok().put("url" , url);
    }
}
